<?php 
import('admin.Action.AdmAction');
class AdminAction extends AdmAction {
	
	function _initialize(){
		parent::_initialize();
		$menulist = array(
			'index'     => '贴子列表',
			'category'  => '分类管理',
		);
		$this->assign('menulist',$menulist);
	} 

	function index(){
		$dao	=	D('Topic');
		$limit  = isset($_POST['limit'])?$_POST['limit']:20;
		$field  = isset($_POST['field'])?$_POST['field']:'cTime';
		$order  = isset($_POST['order'])?$_POST['order']:'desc';		
		//dump($field);dump($order);
		$map['isdel'] = 0;
		//if(!empty($_POST['uid'])) $map['uid'] = $_POST['uid']; 		
		if(!empty($_POST['title'])) $map['title'] = array('like', "%$_POST[title]%");
		$list	=	$dao->where($map)->order($field.' '.$order)->findpage($limit);
		//dump($dao->getLastSql());
		//dump($list);
		
		$this->assign($_POST);
		$this->assign($list);		
		$this->display();
	}
	
	function delete(){
		$id = $_GET['id'];
		if(empty($id)){
			$this->error('请选择删除的帖子');
		}
		
		$map['id'] = array('in', $id);		
		$res = D('Topic')->where($map)->setField( 'isdel',1);
		if($res){
			$this->success('删除帖子成功');
		}else{
			$this->error('删除帖子失败');
		}
	}
	

    public function setAdminBefore(){    	
    	$cid = $_GET['v1'].','.$_GET['v2'].','.$_GET['v3'];
    	$type = isset($_GET['type'])?$_GET['type']:'m';
    	if(empty($_GET['user'])){
    		$cate = model('Xcate')->getTree('bbs',$cid);
	    	$this->assign('cate',$cate);
	    	
	    	//取出本分类下的管理员
	    	$admin     = $cate[0][$type];
    	}else{
    		$admin  = $_GET['user'];
    	}
    	
    	
    	//取出这个管理员的需要操作的信息
    	$adminUid   = strpos($admin,',') === false ? $admin:explode(',',$admin);
    	$map['uid'] = array('in',$adminUid);
    	$user = D('User')->where($map)->field('fullname,uid,uname')->findAll();
    	//处理uname
    	foreach($user as &$value){
    		$uname = preg_split("//",$value['uname'],2,PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
			$value['uname'] = $uname[1];
    	}
    	
    	$cateTitle = model('Xtag')->getTagName($cid);
    	$cateTitle = explode(',',$cateTitle);
    	foreach ($cateTitle as $v){
    		if($v) $ca[] = $v;
    	}
    	$cateTitle = implode(' -> ',$ca);
    	
    	$this->assign('type',$type);
    	$this->assign('cid',str_replace(',','_',$cid));
    	$this->assign('userList',$user);
    	$this->assign('userId',$adminUid);
    	$this->assign('category',$cateTitle);
    	$this->display();
    }
    
    function deleteCategory(){
    	$cids = trim( $_GET['cid'] );
    	$cate = model('Xcate')->getTree('bbs',$cids);
    	$cate = $cate[0];

    	if( is_array( $cate['d'] ) ){
     		$deInfo['error']   = 'havechild';
    		$deInfo['message'] = '此版块存在子版块，请先移除子版块';    		
    	}elseif( $topicnum = D('Topic')->where("tagid=".end(explode(',',$cids)))->count() ){
     		$deInfo['error']   = 'havetopic';
    		$deInfo['message'] = '此版块存在 '.$topicnum.' 个主题,请先将主题转移';
    		
	  		$cate = model('Xcate')->getTree('bbs','0');
			$json = json_encode($cate);
			$this->assign('json',$json); 
    	}else{
    		$deInfo['message'] = '确认要删除此版块吗'; 
    	}
    	
    	$this->assign('deInfo',$deInfo);
    	$this->assign('cids',$cids);
    	$this->display();
    }
    
    
    function doMoveTopic(){
    	$strOldClass = $_POST['oldClass'];
    	$strNewClass = $_POST['newClass'];
    	
		$arrBoard = explode( ',',$strNewClass );
		foreach($arrBoard as $key=>$value){
			$data['class'.($key+1)] = $value;
		}
		$data['tagid'] = end( $arrBoard );
		$data['board'] = $strNewClass;
		
		$map['tagid'] = end( explode(',',$strOldClass) );
		$map['board'] = $strOldClass ;
		
		if( D('Topic')->where($map)->data($data)->save() ){
			echo '1';
		}else{
			echo '0';
		}
		
    }
    
    public function setAdmin(){
    	$intTagId = intval( $_GET['tagid'] );
    	$strBanzhu = D('Board')->where('tagid='.$intTagId)->field('banzhu')->find();
    	$strBanzhu = $strBanzhu['banzhu'];
    	if($strBanzhu){
    		$arrBanzhu = explode(',',$strBanzhu);
	     	$map['uid'] = array('in',$arrBanzhu);
	    	$user = D('User')->where($map)->field('fullname,uid,uname')->findAll();   		
    		$admin['userId'] = $arrBanzhu;
    		$admin['userList'] = $user;
    	}
    	$admin['tagid'] = $intTagId;

    	$this->assign( $admin );
    	$this->display();
    }
    
    function dosetAdmin(){
    	$intTagid = intval($_POST['tagid']);
    	$selectUid = implode( ',', $_POST['selectUid'] );
    	$reslut = D('Board')->saveBanZhu( $intTagid,$selectUid );
    	if($reslut){
    		$this->success( implode(',',array_map('getUserName',$_POST['selectUid'])) );    		
    	}else{
    		$this->error('添加失败');
    	}
    	
    }
    
	/**
	 * category
	 * 管理分类
	 * @access public
	 * @return void
	 */
	public function category() {
		$cate	=	model('Xcate')->getTree('bbs');
		//$result	=	model('Xcate')->saveTree('category:ask',$cate);
		$dao = model('Xdata');
		$cate	=	$dao->get('category:bbs');
		$this->assign('catetree',$cate);
		$this->display();
	}
	
	/**
	 * doChangeCate
	 * 保存分类修改
	 * @access public
	 * @return void
	 */
	public function doChangeCate() {
		//send_http_header('utf8');
		$dao = model('Xdata');
		$cate_t	=	$dao->get('category:bbs');
		
		//F('cate_back_'.date('YmdHis',time()),$cate_t,'data/');
		
		$cate	=	$_POST['cate'];
		$cate   =   sortByCol($cate, 'o', $dir = SORT_ASC);
		set_time_limit(0);
		$user = getMultiArraySubByKey($cate,'e');
		$user = array_unique($user);
		$temp_user = array();
		foreach($user as $value){
			if(strpos($value,',') === false ){
				$temp_user[] = $value;
			}else{
				$t_user = explode(',',$value);
				$temp_user = array_merge($temp_user,$t_user);
			}
		}
		$user = array_unique($temp_user);
		
		$userGroup = model('UserGroup');

		
		foreach($user as $value){
			if($value ==0) continue;
			if($userGroup->inGroup($value,190)) continue;
			$userGroup->addToGroup($value);
			
		}
		
		foreach($cate as &$value){
			$value['d'] = sortByCol($value['d'], 'o', $dir = SORT_ASC);
		}
		$result	=	model('Xcate')->saveTree('category:bbs',$cate);
		
		if($result){
			D('Log','admin')->addLog($this->mid, '', 'bbs_cate');
		}

		$jumpUrl = __URL__.'&category';
		$this->redirect('bbs/admin/category');
		exit;
		if(!$result){
		}else{
			$this->assign('jumpUrl',$jumpUrl);
			$this->success('修改成功！');
		}
	}	
	
}
?>